package com.kingyea.mobilepolice.apimanage.interfaceflow.service.impl;

import com.alibaba.fastjson.JSON;
import com.google.common.collect.Maps;
import com.kingyea.mobilepolice.apimanage.interfaceflow.bean.InterfaceFlow;
import com.kingyea.mobilepolice.apimanage.interfaceflow.bean.InterfaceFlowVo;
import com.kingyea.mobilepolice.apimanage.interfaceflow.dao.InterfaceFlowMapper;
import com.kingyea.mobilepolice.apimanage.interfaceflow.service.InterfaceFlowService;
import com.kingyea.mobilepolice.apimanage.systemsync.Send2Queue;
import com.kingyea.mobilepolice.apimanage.sysvo.bean.AllCondition;
import com.kingyea.mobilepolice.apimanage.utils.ExceptionConditions;
import com.kingyea.mobilepolice.apimanage.utils.StateCode;
import com.kingyea.mobilepolice.apimanage.utils.page.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author cxc
 * @version v1.0.0
 * @description
 * @date Created in 2018-05-05 19:45
 */

@Service
public class InterfaceFlowServiceImpl implements InterfaceFlowService {

    @Autowired
    private InterfaceFlowMapper interfaceFlowMapper;

    @Send2Queue
    @Override
    public boolean save(InterfaceFlow obj) {
        return interfaceFlowMapper.save(obj) == 1;
    }

    @Override
    public PageInfo<InterfaceFlowVo> search(AllCondition condition) throws Exception {
        ExceptionConditions.checkNotNull(condition, StateCode.NO_ARGUMENT_ERROR);
        ExceptionConditions.checkNotNull(condition.getType(), StateCode.NO_ARGUMENT_ERROR);
        PageInfo.startPage(JSON.parseObject(JSON.toJSONString(condition), Maps.<String, Object>newHashMap().getClass()));
        if (1 == condition.getType()) {
            return PageInfo.getPageInfo(interfaceFlowMapper.searchOfRequester(condition));
        }
        return PageInfo.getPageInfo(interfaceFlowMapper.searchOfProvider(condition));
    }

    @Override
    public PageInfo<InterfaceFlowVo> searchGather(AllCondition condition) throws Exception {
        ExceptionConditions.checkNotNull(condition, StateCode.NO_ARGUMENT_ERROR);
        ExceptionConditions.checkNotNull(condition.getType(), StateCode.NO_ARGUMENT_ERROR);
        PageInfo.startPage(JSON.parseObject(JSON.toJSONString(condition), Maps.<String, Object>newHashMap().getClass()));
        if (1 == condition.getType()) {
            return PageInfo.getPageInfo(interfaceFlowMapper.searchGatherOfRequester(condition));
        }
        return PageInfo.getPageInfo(interfaceFlowMapper.searchGatherOfProvider(condition));
    }

    /*static{ //骚操作
        ServiceInfo serviceInfo = new ServiceInfo();
        final List<ServiceInfo> serviceInfoList = Lists.newArrayList();
        Lists.newArrayList(condition.getUrl(), condition.getServiceName()).stream().filter(StringUtils::isNotBlank).findFirst().ifPresent(any -> {
            serviceInfoList.addAll(serviceInfoService.list(serviceInfo));
        });
        condition.setTypeUrlIds(serviceInfoList.parallelStream().map(ServiceInfo::getId).distinct().collect(Collectors.toList()));

        final List<Requester> requesterList = Lists.newArrayList();
        final List<Provider> providerList = Lists.newArrayList();
        if(Integer.valueOf(1).equals(condition.getType())){
            Requester bean = new Requester();
            bean.setRequester(condition.getTypeName());
            requesterList.addAll(requesterService.list(bean));
            condition.setTypeIds(requesterList.stream().map(Requester::getId).distinct().collect(Collectors.toList()));
        }else if(Integer.valueOf(2).equals(condition.getType())){
            Provider bean = new Provider();
            bean.setProvider(condition.getTypeName());
            providerList.addAll(providerService.list(bean));
            condition.setTypeIds(providerList.stream().map(Provider::getId).distinct().collect(Collectors.toList()));
        }
        PageInfo.startPage(JSON.parseObject(JSON.toJSONString(condition), Maps.<String, Object>newHashMap().getClass()));
        List<InterfaceFlowVo> voList = interfaceFlowMapper.search(condition).stream().map(bean -> {
            InterfaceFlowVo vo = new InterfaceFlowVo();
            BeanUtils.copyProperties(bean, vo);
            serviceInfoList.stream().filter(info -> info.getId().equals(bean.getTypeUrlId())).findFirst().ifPresent(info -> vo.setServiceUrl(info.getPath()));
            if (null != requesterList && !requesterList.isEmpty()) {
                requesterList.stream().filter(req -> req.getId().equals(vo.getTypeId())).findFirst().ifPresent(req -> vo.setTypeName(req.getRequester()));
            } else if (null != providerList && !providerList.isEmpty()) {
                providerList.stream().filter(pro -> pro.getId().equals(vo.getTypeId())).findFirst().ifPresent(pro -> vo.setTypeName(pro.getProvider()));
            }
            return vo;
        }).collect(Collectors.toList());
        return PageInfo.getPageInfo(voList);
    }*/
}
